<?php
namespace app\weixin\controller;
use think\Controller;
use helper\Taobaoapi;

class Service extends Auth
{
    protected function _initialize()
    {    
         //parent::_initialize();
    
    }

    //查询商品结果
    public function shop(){
        $keyword = input('param.keyword');
        $keyword = str_replace('￼','',$keyword);
        $this->assign([
          'site_title'=>$keyword?$keyword:'商品列表',
          'keyword'=>$keyword,
          'bar_active'=>'search',
        ]); 
        return $this->fetch(); 
    }

    //获取淘口令
    public function tkl(){
              $itemid = input('param.itemid');
              $url = 'https://item.taobao.com/item.htm?id='.$itemid;
              $weixin = new \weixin\Weixin();
              $timestamp = $weixin->getunixtime13();
              $sign = $weixin->sign($url,$timestamp);
              $url = urlencode($url);
              $url = $weixin->tkzlserver().'taokeapi/getdata?timestamp='.$timestamp.'&sign='.$sign.'&q='.$url;
              //$url = $weixin->tkzlserver()."DemoService/Client/search.json?timestamp={$timestamp}&sign={$sign}&q={$url}";
              $data = $weixin->get_redirect_url($url);
              if(!empty($data) && $data!="[]"){
                  $data = json_decode($data,true);
                  if(isset($data[0])){
                     $data = $data[0];
                     $data['commission'] = intval($data['commission']*100)/100;
                     $data['coupon_price'] = $data['isCoupon']=='True'?$data['itemPrice']-$data['discountedPrice']:$data['itemPrice'];//券后价
                  }
                  return json(array('code'=>0,'result'=>$data));
              }else{
                  return json(array('code'=>101,'result'=>''));       
              }
    }   

}
